﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using Spring.Objects.Factory.Support;
using Strongshell.Recoil.Core.Composition.Parsing.DependencyInjection;

namespace Strongshell.Recoil.Core.Composition.Parsing.Handlers
{
    public class NewArrayInitExpressionHandler : IExpressionHandler
    {
        private readonly IExpressionParser expressionParser;
        private readonly IObjectConfigurationRegistry objectConfigurationRegistry;

        public NewArrayInitExpressionHandler(IObjectConfigurationRegistry objectConfigurationRegistry, IExpressionParser expressionParser)
        {
            this.objectConfigurationRegistry = objectConfigurationRegistry;
            this.expressionParser = expressionParser;
        }

        public void Handle(IDependencyInjector currentInjector, Expression currentExpression)
        {
            var arrayInitExpression = (NewArrayExpression)currentExpression;

            var managedList = new ManagedList();
            currentInjector.Inject(managedList);

            foreach (var element in arrayInitExpression.Expressions)
            {
                expressionParser.Parse(new ListDependencyInjector(objectConfigurationRegistry,
                                                                  currentInjector.SelectObjectConfiguration(),
                                                                  element.Type,
                                                                  managedList),
                                       element);
            }
        }
    }
}
